package com.lb.uts.quartz;

import com.lb.uts.constants.UTSConstants;
import com.lb.uts.service.IJobRecordService;
import com.lb.uts.util.RscResponseUtils;
import com.lb.uts.util.SpringContextUtil;
import org.apache.http.HttpResponse;
import org.apache.http.concurrent.FutureCallback;


/**
 * 异步回调处理
 * 主要是处理结果
 */
public class ExecuteCallBack implements FutureCallback<HttpResponse> {

    private String systemName;

    private String host;

    private String jobRecordId;

    public String getSystemName() {
        return systemName;
    }

    public void setSystemName(String systemName) {
        this.systemName = systemName;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getJobRecordId() {
        return jobRecordId;
    }

    public void setJobRecordId(String jobRecordId) {
        this.jobRecordId = jobRecordId;
    }

    public ExecuteCallBack() {

    }

    public ExecuteCallBack(String systemName, String host, String jobRecordId) {
        this.systemName = systemName;
        this.host = host;
        this.jobRecordId = jobRecordId;
    }

    @Override
    public void completed(HttpResponse result) {
        RscResponseUtils.parseResponse(result, "utf-8");

        int status = result.getStatusLine().getStatusCode();
        if (status == 200)
            SpringContextUtil.getBean(IJobRecordService.class).updateSuccessRecord(systemName, UTSConstants.REMOTECALL_STTAUS_EXECUTE_SUCCESS, host, jobRecordId, result.getEntity().toString());
        else if (status != 404) {
            SpringContextUtil.getBean(IJobRecordService.class).updateSuccessRecord(systemName, UTSConstants.REMOTECALL_STTAUS_EXECUTE_FAIL, host, jobRecordId, result.toString());
        } else
            SpringContextUtil.getBean(IJobRecordService.class).updateFailRecord(systemName, host, jobRecordId, result.toString());
    }

    @Override
    public void failed(Exception ex) {
        //任务失败，修改执行记录
        SpringContextUtil.getBean(IJobRecordService.class).updateFailRecord(systemName, host, jobRecordId, ex.toString());
    }

    @Override
    public void cancelled() {

    }
}
